/**
 * Copyright (c) 2006-2007 Jinyi.lu
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of GNU LGPL license by gun.org
 * which accompanies this distribution, and is available at
 * http://gnu.feld-it.at/copyleft/lesser.html
 * 
 * Project Homepage : http://code.google.com/p/demou
 * Source Code: svn checkout http://demou.googlecode.com/svn/trunk/ demou
 * Mailing list : jinyi.lu@gmail.com
 *                stonelu@126.com
 *
 *  
 * Contributors:
 *     JinYi.Lu - initial API and implementation
 */
package cn.tworen.demou.remoting;

import java.util.Arrays;
import java.util.List;

import net.sf.cindy.PacketDecoder;
import net.sf.cindy.PacketEncoder;
import net.sf.cindy.Session;
import net.sf.cindy.SessionAcceptor;
import net.sf.cindy.SessionAcceptorHandler;
import net.sf.cindy.SessionType;
import net.sf.cindy.session.SessionFactory;

import org.apache.log4j.Logger;

import cn.tworen.demou.Config;

/**
 * Created on  2007-2-4
 *
 * Title       : HttpServer.java
 * Description : 
 * 
 * @author     : LuJinYi
 * @version    : 1.0
 * @Date       : 2007-2-4
 * History     : 
 * 
 */
public class HttpServer {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(HttpServer.class);

    private static final PacketEncoder encoder = new HttpResponseEncoder();
    private static final PacketDecoder decoder = new HttpRequestDecoder();
    
    public static void main(String[] args) throws Exception {
    	logger.info("Http serve init");
        List params = Arrays.asList(args);

        SessionAcceptor acceptor = SessionFactory
                .createSessionAcceptor(SessionType.TCP);
        acceptor.setListenPort(Config.getInstance().readInt("http.server.port"));
        logger.info("Listner prot:" + acceptor.getListenPort());
        
        
        acceptor.setAcceptorHandler(new SessionAcceptorHandler() {

            public void exceptionCaught(SessionAcceptor acceptor,
                    Throwable cause) {
                System.err.println(cause);
            }

            public void sessionAccepted(SessionAcceptor acceptor,
                    Session session) throws Exception {
            	logger.debug("###sessionAccepted");
            	session.setPacketDecoder(decoder);
            	session.setPacketEncoder(encoder);
            	session.setSessionHandler(new AmfSessionHandler());
            	session.start();
            }
        });
        acceptor.start();
        logger.info("http server start!");
    }
}
